package com.huawei.M01.iterator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 返回字符串所有可能的回文排列字符串
 *
 * @author 111
 */
public class GeneratePalindromes {
    public List<String> generatePalindromes(String s) {
        List<String> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        char[] strings = s.toCharArray();
        boolean[] used = new boolean[strings.length];
        Arrays.sort(strings);
        dfs(strings, 0, used, result, sb);
        return result;
    }

    private void dfs(char[] strings, int index, boolean[] used, List<String> result, StringBuilder sb) {
        if (index == strings.length) {
            if (valid(sb.toString())) {
                result.add(new String(sb));
            }
            return;
        }
        // 循环寻找满足条件的所有组合
        for (int i = 0; i < strings.length; i++) {
            if (!used[i]) {
                if (i > 0 && strings[i] == strings[i - 1] && used[i - 1]) {
                    continue;
                }
                sb.append(strings[i]);
                used[i] = true;
                dfs(strings, index + 1, used, result, sb);
                // 回溯
                used[i] = false;
                sb.deleteCharAt(sb.length() - 1);
            }
        }
    }

    private boolean valid(String sb) {

        return true;
    }

    public static void main(String[] args) {
        GeneratePalindromes generatePalindromes = new GeneratePalindromes();
        System.out.println(generatePalindromes.generatePalindromes("abcadcdabcadcd"));
    }
}
